/**
 * User: 蕨菜
 * Date: 2019-05-08
 * Time: 14:52
 * Description: 此文件简要说明
 */


const sequelize = require('../mysql/_db');

const Class = sequelize.import('./Class');
const Grade = sequelize.import('./Grade');
const Teacher = sequelize.import('./Teacher');
const Student = sequelize.import('./Student');
const Project = sequelize.import('./Project');
const Title = sequelize.import('./Title');
const Role = sequelize.import('./Role');
const Score = sequelize.import('./Score');

const TeacherClass = sequelize.import('./TeacherClass');
const GradeClass = sequelize.import('./GradeClass');
const TeacherRole = sequelize.import('./TeacherRole');
const TeacherTitle = sequelize.import('./TeacherTitle');

// foreignKey可以不写, 会自动添加一个字段作为外键, 命名方式根据model(模型)和primaryKey(主键)相关, 配置中如果开启下划线就是 model_primary, 否则就是 modelPrimary
// 老师和班级的多对多关系
Class.belongsToMany(Teacher, {
    through: TeacherClass
});
Teacher.belongsToMany(Class, {
    // 这儿如果设置了as:myClass, 那Teacher实例调用的时候就要 addMyClasses getMyClasses等,
    // 也就是说这里设置了方法的别名, 并且查询出来的结果也将以这个字段命名,
    /**
     * 比如 teacher.getMyClasses(),但是这种查询出来的数据会携带关联维表,目前我不知道怎么去掉这个关联维表
     * 所以现在还在用findAll或者findOne来查询
     */
    through: TeacherClass, as: 'myClasses'
});

// 老师和角色的多对多关系
Teacher.belongsToMany(Role, {
    through: TeacherRole
})
Role.belongsToMany(Teacher, {
    through: TeacherRole
})

// 老师和头衔的多对多关系
Teacher.belongsToMany(Title, {
    through: TeacherTitle
})
Title.belongsToMany(Teacher, {
    through: TeacherTitle
})

// 年级和班级的多对多关系
Grade.belongsToMany(Class, {
    through: GradeClass
});
Class.belongsToMany(Grade, {
    through: GradeClass
});

// 班级和学生的一对多关系
Class.hasMany(Student);
Student.belongsTo(Class)

// 年级和老师的一对多关系
Grade.hasMany(Teacher);
Teacher.belongsTo(Grade);

// 年级和学生的一对多关系
Grade.hasMany(Student);
Student.belongsTo(Grade);

// 年级和学科的一对多关系
Grade.hasMany(Project);
Project.belongsTo(Grade);

// 学科和老师的一对多关系
Project.hasMany(Teacher);
Teacher.belongsTo(Project);

// 学生和分数一对多, 班级和分数一对多, 年级和分数一对多
Score.belongsTo(Student);
Student.hasMany(Score);

Score.belongsTo(Class);
Class.hasMany(Score);

Score.belongsTo(Grade);
Grade.hasMany(Score);

sequelize.sync();

module.exports = {
    Grade,
    Class,
    Student,
    Teacher,
    TeacherClass,
}
